<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属年份:" label-width="140px">
        <el-date-picker
          v-model="queryParams.setYear"
          type="year"
          value-format="YYYY"
          placeholder="请选择年份"
        />
      </el-form-item>
      <el-form-item label="节假日类型:" label-width="140px">
        <el-select v-model="queryParams.holidayType" placeholder="请选择假期类型">
          <el-option
            v-for="dict in holidayTypeOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />

      <el-table-column label="设定年份" width="180" prop="setYear" />

      <el-table-column label="节假日名称" prop="holidayName" />
      <el-table-column label="包含日期" width="180" prop="holidayDates" />
      <el-table-column label="创建人" width="180" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" width="180" prop="updatedBy" />
      <el-table-column label="编辑时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--新增假期类型-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="设定节假日年份:" label-width="140px" prop="setYear">
              <el-date-picker
                v-model="form.setYear"
                type="year"
                value-format="YYYY"
                placeholder="请选择年份"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!--编辑假期类型-->
    <el-dialog :title="titleUpdate" v-model="openUpdate" width="60%" append-to-body>
      <el-form ref="formUpdateRef" :model="formUpdate" :rules="rulesUpdate">
        <el-row
          :gutter="12"
          style="display: flex; flex-direction: row; justify-content: center"
        >
          <span style="color: red; font-weight: 800">{{ formUpdate.setYear }}</span>
          年-
          <span style="color: red; font-weight: 800">{{ formUpdate.holidayName }}</span
          >的日期设定
        </el-row>
        <el-row style="margin-top: 12px">
          <el-form-item label="所含日期:" label-width="140px">
            <el-date-picker
              size="large"
              v-model="formUpdate.holidayDates"
              type="dates"
              placeholder="请选择一个或多个日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitUpdate">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Holiday">
import { listHoliday, addHoliday, getHoliday, updateHoliday } from "@/api/system/holiday";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const openUpdate = ref(false);
const titleUpdate = ref("");
const { proxy } = getCurrentInstance();
const holidayTypeOption = ref([
  { id: "1", label: "春节假期" },
  { id: "2", label: "清明节假期" },
  { id: "3", label: "五一劳动节假期" },
  { id: "4", label: "中秋节假期" },
  { id: "5", label: "国庆节假期" },
  { id: "6", label: "元旦假期" },
]);

const data = reactive({
  form: {},
  formUpdate: {},
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {
    setYear: [
      {
        required: true,
        message: "节假日年份设定必须选择！",
        trigger: ["blur", "change"],
      },
    ],
  },
  rulesUpdate: {
    holidayDates: [
      {
        required: true,
        message: "改类型下节假日或法定工作日必须选择！",
        trigger: ["blur", "change"],
      },
    ],
  },
});
const { queryParams, form, rules, formUpdate, rulesUpdate } = toRefs(data);

/** 获取列表数据 */
function getList() {
  loading.value = true;
  listHoliday(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}

/** 处理新增 */
function handleAdd() {
  open.value = true;
  title.value = "新增某年法定节假日工作日信息";
  reset();
}

/** 处理编辑 */
function handleUpdate(row) {
  getHoliday(row.id).then((response) => {
    openUpdate.value = true;
    titleUpdate.value = "编辑某年法定节假日工作日信息";
    formUpdate.value = response.data;
  });
}
/** 处理取消 */
function cancel() {
  open.value = false;
  openUpdate.value = false;
}

/** 处理数据编辑 */
function submitUpdate() {
  proxy.$refs["formUpdateRef"].validate((valid) => {
    if (valid) {
      if (formUpdate.value.id != undefined) {
        formUpdate.value.holidayDates = formUpdate.value.holidayDates + "";
        updateHoliday(formUpdate.value).then((response) => {
          proxy.$modal.msgSuccess("修改法定节假日工作日信息成功");
          openUpdate.value = false;
          getList();
        });
      }
    }
  });
}
/** 处理数据提交 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      addHoliday(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增法定节假日工作日信息成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    current: 1,
    size: 10,
  };
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();
</script>
